﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Messaging;

namespace TPOJ.MSMQMessaging
{
    public class QueueMessage:TPOJQueue , TPOJ.IMessaging.IQueueMessage
    {
        private static readonly string queuePath = ConfigurationManager.AppSettings["SubmissionQueuePath"];
        private static int timeoutMilliseconds = 200;

        public QueueMessage()
            : base(queuePath, timeoutMilliseconds)
        {
            queue.Formatter = new XmlMessageFormatter();
        }

        public TPOJ.Model.Message Receive()
        {
            // This method involves in distributed transaction and need Automatic Transaction type
            base.transactionType = MessageQueueTransactionType.Single;
            return (TPOJ.Model.Message)((Message)base.ReceiveMessage()).Body;
        }

        public TPOJ.Model.Message Receive(int timeout)
        {
            base.timeout = TimeSpan.FromSeconds(Convert.ToDouble(timeoutMilliseconds));
            return Receive();
        }

        public void Send(TPOJ.Model.Message message)
        {
            // This method does not involve in distributed transaction and optimizes performance using Single type
            base.transactionType = MessageQueueTransactionType.Single;
            base.SendMessage(message);
        }
    }
}
